<?php
namespace Components\Parameters\Sha;

class Factory {
  protected $_instance = [];
  protected $_parameter = [];

  public function __construct(array $parameter) {
    $this -> _parameter = $parameter;
  }

  public function getParameter($key = null) {
    if(is_null($key)) {
      return $this -> _parameter;
    }

    return isset($this -> _parameter[$key])?$this -> _parameter[$key]:[];
  }

  protected function newClassName($key, $class_name, $parameter) {
    if(isset($this -> _instance[$key]) && !empty($this -> _instance[$key])) { return $this -> _instance[$key]; }

    $className = __NAMESPACE__ . '\\' . $class_name;
    if(!class_exists($className)) { throw new \Exception($className . ' is non-existent.'); }
 
    return $this -> _instance[$key] = new $className($parameter);
  }

  use \Components\Traits\FactoryTraits;
}
